'use strict'
app.controller('DatasourceController', function($scope, $http, $timeout, time, obj) {


    $scope.api = $scope.serverUrl + "web/datasource";

    $scope.query = {
        "size": 10,
        "page": 1,
        "name": "",
        "type": 0
    };
    
    $scope.form = {
        "isAdd": true, //true表示新增，false表示修改
        "title": '新增',
        "entity": {
            "id": '',
            "name": '',
            "account": '',
            "password": '',
            "url": '',
            "driver": ''
        }
    };



    $scope.body = {
        "queryTitle": "查询条件",
        "tableTitle": "数据列表"
    };


    let tbl;
    let layer = layui.layer

    $timeout(function(){
        tbl = $('#tbl').DataTable({
            "ajax" : function (data, callback, settings) {
                //封装请求参数
                $scope.query.size = data.length;//页面显示记录条数，在页面显示每页显示多少项的时候
                $scope.query.page = (data.start / data.length)+1;//当前页码
                Object.keys($scope.query).forEach(function(key) {
                    let val = $scope.query[key]
                    if(val === ""){
                        $scope.query[key] = undefined;
                    }
                });
                let returnData = {};
                returnData.recordsTotal = 0;//返回数据全部记录
                returnData.recordsFiltered = 0;//后台不实现过滤功能，每次查询均视作全部结果
                returnData.data = [];//返回的数据列表
                $http.post($scope.api + '/page/list', $scope.query).then(function(res){
                    // console.log(res.data);
                    let data = res.data;
                    if(data.code === 1){
                        returnData.recordsTotal = data.data.total;//返回数据全部记录
                        returnData.recordsFiltered = data.data.total;//后台不实现过滤功能，每次查询均视作全部结果
                        returnData.data = data.data.records;//返回的数据列表
                    }
                    callback(returnData);
                },
                function(err){
                    alert("数据加载请求异常");
                });
            },
            "serverSide":true,
            "language":$scope.datatables_lang, //提示信息
            "renderer": "bootstrap", //渲染样式：Bootstrap和jquery-ui
            "pagingType": "full_numbers", //分页样式：simple,simple_numbers,full,full_numbers
            "bFilter": true,
            "bSort": false,
            /*"scrollX": true,*/
            "fnInitComplete":function (oSettings, json) {
            
            },
            "columns": [
                {
                    "targets": 0,
                    "render": function(data, type, row){
                        return '<label class="i-checks m-b-none"><input type="checkbox"><i></i></label>';
                    }
                },
                {"data": "name"},
                {"data": "account"},
                {"data": "driver"},
                // {"data": "url"},
                {
                    "render": function(data, type, row){
                        return time.format(row.createTime);
                    }
                },
                {
                    "render": function(data, type, row){
                        let html = '<span class="btn btn-info btn-sm" name="edit">编辑</span>';
                        html += '<span class="btn btn-danger btn-sm" style="margin-left:3px;" name="del">删除</span>';
                        return html;
                    }
                },
            ]
        })
    }, 0);

    $('#tbl tbody').on('click', 'span[name="edit"]', function(e){
        let entity = tbl.row($(e.target).parents("tr")).data();
        $scope.form.isAdd = false;
        $scope.form.title = '修改';
        obj.copy(entity, $scope.form.entity);
        $scope.$apply();
        $("#add").modal("show");
    });

    $('#tbl tbody').on('click', 'span[name="del"]', function(e){
        let entity = tbl.row($(e.target).parents("tr")).data();
        // console.log(entity);
        // layer.msg('hello world', {icon: 1});
        $http.delete($scope.api + "/" + entity.id, {}).then(function(response){
            tbl.ajax.reload();
        },
        function(err){
            toaster.pop('info', "title", "请求异常");
        });
    });

    $scope.add = function(){
        $scope.form.isAdd = true;
        $scope.form.title = '新增';
        $scope.form = {
            "isAdd": true, //true表示新增，false表示修改
            "title": '新增',
            "entity": {
                "id": '',
                "name": '',
                "account": '',
                "password": '',
                "url": '',
                "driver": ''
            }
        };
        $("#add").modal("show");
    };

    $scope.search = function(){
        tbl.ajax.reload();
    };

    $scope.reset = function(){
        $scope.query = {
            "size": 10,
            "page": 1
        };
        tbl.ajax.reload();
    }

    $scope.save = function(){
        let url =  $scope.api + "/";
        if($scope.form.isAdd){
            $http.post(url, $scope.form.entity).then(function(res){
                tbl.ajax.reload();
                $("#add").modal("hide");
            },
            function(err){

            });
        }else{
            $http.post(url, $scope.form.entity).then(function(res){
                tbl.ajax.reload();
                $("#add").modal("hide");
            },
            function(err){

            });
        }
    }

    $scope.test = function(){
        let url =  $scope.api + "/test";
        $http.post(url, $scope.form.entity).then(function(res){
            if(res.data.code === 1){
                layer.msg('连接成功', {icon: 1});
            }else{
                layer.msg('连接失败，原因:' + res.data.msg, {icon: 1});
            }
        },
        function(err){

        });
    }

});